import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import Components from 'unplugin-vue-components/vite'
import AutoImport from 'unplugin-auto-import/vite'
// import IconsResolver from 'unplugin-icons/resolver'
// import Icons from 'unplugin-icons/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vite.dev/config/
export default defineConfig(({ mode, command }) => {

  let env = loadEnv(mode, process.cwd() + "/.env")
  console.log(env);

  const pathSrc = path.resolve(__dirname, 'src');
  const pathAssets = path.resolve(__dirname, 'src/assets');
  const pathComponet = path.resolve(__dirname, 'src/components');
  const layouComponet = path.resolve(__dirname, 'src/Layout/components');
  console.log(pathSrc);
  console.log(pathAssets);
  console.log(pathComponet);

  return {
    plugins: [
      //vue插件,官方自带识别.vue文件
      vue(),
      AutoImport({
        resolvers: [
          ElementPlusResolver(),
          // IconsResolver({
          //    prefix: 'Icon',
          // })
        ],
        imports: ['vue', 'vue-router', 'pinia']
      }),
      Components({
        dirs: [pathComponet,layouComponet],
        resolvers: [
          //在使用icon图标是不能直接使用，比如<Edit />,需要写成<i-ep-edit /> {prefix}-{collection}-{icon}
          // IconsResolver({
          //    enabledCollections: ['ep']
          // }),
          //按需自动导入element-plus组件
          ElementPlusResolver()
        ]
      }),
      // Icons({
      //   autoInstall: true,
      // }),
    ],
    resolve: {
      alias: {
        '@': pathSrc,
        '@assets': pathAssets,
        '@components': pathComponet
      }
    },
    server: {
      port: 8888,
      open: true, // auto open the windows
      host:true,
      // proxy: {
      //   '/api': {
      //     target: 'http://localhost:5042/',
      //     changeOrigin: true,
      //   }
      // }
    },
    css: {},
    build: {
      outDir: 'dist',
      sourcemap: false,
      rollupOptions: {
        output: {
          chunkFileNames: 'assets/js/[name]-[hash].js',
          entryFileNames: 'assets/js/[name]-[hash].js',
          assetFileNames: 'assets/[ext][name]-[hash].[ext]'
        }
      }
    },
    optimizeDeps: {
      include: ['element-plus']
    },
    define: {
      'process.env': process.env
    }
  }
})
